package com.rocksea.app.db.uploadflag;

import android.content.Context;
import android.os.Environment;

import android.database.sqlite.SQLiteDatabase;
import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.DataBaseConfig;
import com.litesuits.orm.db.assit.QueryBuilder;
import com.litesuits.orm.db.assit.SQLiteHelper;
import com.litesuits.orm.db.assit.WhereBuilder;
import com.litesuits.orm.db.model.ColumnsValue;
import com.litesuits.orm.db.model.ConflictAlgorithm;


import java.util.ArrayList;
import java.util.Date;

/**
 * @author Administrator
 * @date 2017/11/29
 */

public class UploadFlagDb implements SQLiteHelper.OnUpdateListener
{

    public static LiteOrm mUploadDb;
    private static UploadFlagDb mUploadFlagDb;

    public UploadFlagDb(Context context)
    {
        DataBaseConfig config = new DataBaseConfig(context);
        config.dbName = Environment.getExternalStorageDirectory().getPath()+"/RailDatabase/db/UploadFlag.db";
        config.dbVersion = 1;
        mUploadDb = LiteOrm.newSingleInstance(config);
        //mUploadDb.setDebugged(true);
        UploadFlag flag = new UploadFlag("","");
        mUploadDb.insert(flag);
        mUploadDb.delete(flag);
    }

    public static UploadFlagDb newInstance(Context context)
    {
        if (mUploadFlagDb==null)
        {
            synchronized (UploadFlagDb.class)
            {
                mUploadFlagDb = new UploadFlagDb(context);
            }
        }
        return mUploadFlagDb;
    }

    public void insert(String sectionId,String uuid)
    {
        UploadFlag flag = new UploadFlag(sectionId,uuid);
        mUploadDb.insert(flag);
    }

    public void insertBySql(ArrayList<UploadFlag> uploadFlags)
    {
        mUploadDb.insert(uploadFlags);
    }

    public void delete(String uuid)
    {
        WhereBuilder whereBuilder = new WhereBuilder(UploadFlag.class);
        whereBuilder.where("uuid LIKE ?",uuid);
        mUploadDb.delete(whereBuilder);
    }

    public void deletebySetionId(String sectionId)
    {
        WhereBuilder whereBuilder = new WhereBuilder(UploadFlag.class);
        whereBuilder.andEquals("sectionId",sectionId);
        mUploadDb.delete(whereBuilder);
    }

    public long queryCount(String uuid)
    {
        QueryBuilder<UploadFlag> queryBuilder = new QueryBuilder<>(UploadFlag.class);
        queryBuilder.whereEquals("uuid", uuid);
        return mUploadDb.queryCount(queryBuilder);
    }

    public boolean isUpLoad(String uuid)
    {
        QueryBuilder<UploadFlag> queryBuilder = new QueryBuilder<>(UploadFlag.class);
        queryBuilder.whereEquals("uuid", uuid);
        ArrayList<UploadFlag> uploadFlags = mUploadDb.query(queryBuilder);
        return uploadFlags.size() != 0 && uploadFlags.get(0).upload != 0;
    }

    public void makeUpLoad(String uuid)
    {
        WhereBuilder whereBuilder = new WhereBuilder(UploadFlag.class);
        whereBuilder.andEquals("uuid",uuid);
        ColumnsValue columnsValue = new ColumnsValue(new String[]{"upload"},new Object[]{1});
        mUploadDb.update(whereBuilder, columnsValue, ConflictAlgorithm.Fail);
    }



    @Override
    public void onUpdate(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void query() {
        mUploadDb.query(UploadFlag.class);
    }
}
